/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MailingService;

import javax.ejb.ActivationConfigProperty;
import javax.ejb.MessageDriven;
import javax.jms.MapMessage;
import javax.jms.Message;
import javax.jms.MessageListener;
import BusinessLogic.Constants;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.JMSException;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author James
 */
@MessageDriven(mappedName = "jms/OutgoingMailQueue", activationConfig = {
    @ActivationConfigProperty(propertyName = "acknowledgeMode", propertyValue = "Auto-acknowledge"),
    @ActivationConfigProperty(propertyName = "destinationType", propertyValue = "javax.jms.Queue")
})
public class OutgoingMailBean implements MessageListener {
    //Variables
    private String recipient;
    private String subject;
    private String content;
    private int    msgType;

    public OutgoingMailBean() {}
    
    @Override
    public void onMessage(Message message) {
        MapMessage msg = (MapMessage) message;
        try {
            System.out.println("OutgoingEmailBean: Receive a message");
            msgType = msg.getInt("msgType");
            recipient = msg.getString("recipient");
            System.out.println("OutgoingEmailBean: send email to " + recipient);
            //Based on type of message, decide the content and subject of the email
            switch (msgType) {
                case Constants.MAIL_ACTIVATION:
                    ActivateEmail ae = new ActivateEmail();
                    subject = ActivateEmail.subject;
                    ae.link = msg.getString("link");
                    content = ae.getContent();
                    break;
//                case FORGOT:
//                    ForgotEmail fe = new ForgotEmail();
//                    subject        = ForgotEmail.subject;
//                    if (msg.getString("str1").equals("username")) fe.username = msg.getString("str2");
//                    else fe.password = msg.getString("str2");
//                    content = fe.getContent();
//                    break;
            }

            //Set the properties of mail server
            Properties props = System.getProperties();
            props.put("mail.smtp.host", "smtp.lingua123.com");
            props.put("mail.smtp.user", "testuser");
            props.put("mail.smtp.password", "a1b2c3d4F5");
            props.put("mail.smtp.sasl.enable", "true");
            props.put("mail.smtp.port", "2525");
            props.put("mail.smtp.auth", "true");

            Session session = Session.getDefaultInstance(props, null);
            MimeMessage mail= new MimeMessage(session);
            mail.setFrom(new InternetAddress("testuser@lingua123.com"));
            mail.setRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(recipient));
            mail.setSubject(subject);
            mail.setText(content);

            //Send the message
            Transport transport = session.getTransport("smtp");
            transport.connect("smtp.lingua123.com", 2525, "testuser", "a1b2c3d4F5");
            mail.saveChanges();
            transport.sendMessage(mail, mail.getAllRecipients());
            transport.close();
        } catch (MessagingException ex) {
            Logger.getLogger(OutgoingMailBean.class.getName()).log(Level.SEVERE, null, ex);
        } catch (JMSException ex) {
            Logger.getLogger(OutgoingMailBean.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
